package com.danco.training.zaneuskaya.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.City;
import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.repository.CityDao;
import com.danco.training.zaneuskaya.repository.CountryDao;
import com.danco.training.zaneuskaya.service.CityService;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("cityService")
@Transactional
public class CityServiceImpl implements CityService {

	

	private static final String CITY_DOESN_T_EXIST = "City doesn't exist ";

	private static final Logger logger = Logger
			.getLogger(CityServiceImpl.class);

	@Autowired
	private CityDao cityDao;

	@Autowired
	private CountryDao countryDao;

	public CountryDao getCountryDao() {
		return countryDao;
	}

	public void setCountryDao(CountryDao countryDao) {
		this.countryDao = countryDao;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	@Override
	public void createEntity(City t) {
		try {

			cityDao.create(t);

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public void deleteEntityById(Integer id) {
		try {
			City city = cityDao.read(id);
			
				cityDao.delete(city);
		
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public void updateEntity(City t) {
		try {
			City city = cityDao.read(t.getCityId());
			city.setCityName(t.getCityName());
			city.setCountry(t.getCountry());
			cityDao.update(city);
			
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public City getEntityById(Integer id) {
		City city = null;
		try {
			city = cityDao.read(id);
			if (city == null) {
				throw new NullPointerException(CITY_DOESN_T_EXIST);
			}
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return city;
	}

	@Override
	public List<City> getEntityList() {
		List<City> cities = null;
		try {
			cities = cityDao.readAll();
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return cities;
	}

	@Override
	public List<City> getCityByCountry(Country country) {
		List<City> cities = null;
		try {
			country = countryDao.read(country.getCountryId());
			if (country == null) {
				throw new NullPointerException(CITY_DOESN_T_EXIST);
			}
			cities = cityDao.findCitiesByCountry(country);
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return cities;
	}

}
